package com.next

import grails.transaction.Transactional
import org.grails.web.util.WebUtils

@Transactional
class SubscribeService
{

    def springSecurityService

    def sendSubscribe(Object object)
    {
        def link = WebUtils.retrieveGrailsWebRequest().getBaseUrl()
        // def link = "http://crm.cindata.cn"
        def subject
        def followList
        switch (object)
        {
        case Account:
            link += "/account/"
            subject = "您关注的客户(${object?.name})信息已更新，请及时查看！"
            followList = Follow.findAllByAccount(object)
            break
        case Contact:
            link += "/contact/"
            subject = "您关注的联系人(${object?.fullName})信息已更新，请及时查看！"
            followList = Follow.findAllByContact(object)
            break
        case Opportunity:
            link += "/opportunity/"
            subject = "您关注的商机(${object?.serialNumber})信息已更新，请及时查看！"
            followList = Follow.findAllByOpportunity(object)
            break
        case Activity:
            link += "/activity/"
            subject = "您关注的活动(${object?.name})信息已更新，请及时查看！"
            followList = Follow.findAllByActivity(object)
            break
        case Issue:
            link += "/issue/"
            subject = "您关注的反馈(${object?.subject})信息已更新，请及时查看！"
            followList = Follow.findAllByIssue(object)
            break
        case Asset:
            link += "/asset/"
            subject = "您关注的资产(${object?.name})信息已更新，请及时查看！"
            followList = Follow.findAllByAsset(object)
            break
        case Campaign:
            link += "/campaign/"
            subject = "您关注的市场活动(${object?.name})信息已更新，请及时查看！"
            followList = Follow.findAllByCampaign(object)
            break
        }
        link += "show/" + object?.id
        def subscribe
        followList?.each { follow ->
            subscribe = new Subscribe()
            subscribe.user = follow?.user
            subscribe.source = object?.user
            subscribe.tag = 'Tips'
            subscribe.subject = subject
            subscribe.link = link
            if (subscribe.validate())
            {
                subscribe.save()
            }
            else
            {
                println subscribe.errors
            }
        }
    }

}
